//
//  WaterWareView.m
//  ios 动画
//
//  Created by tepusoft on 16/4/22.
//  Copyright © 2016年 tepusoft. All rights reserved.
//

#import "WaterWareView.h"

@interface WaterWareView()

@property (nonatomic, strong) CADisplayLink *waveDisplaylink;

@property (nonatomic, strong) CAShapeLayer *firstWaveLayer;
@property (nonatomic,strong) CAShapeLayer *secondWaveLayer;

@property (nonatomic, strong) UIColor *firstWaveColor;
@property (nonatomic, strong) UIColor *secondWaveColor;
@property (nonatomic, assign) CGFloat percent;

@end

@implementation WaterWareView
{
    CGFloat waveA;//水纹振幅
    CGFloat waveW ;//水纹周期
    CGFloat offsetX; //位移
    CGFloat currentK; //当前波浪高度Y
    
    CGFloat waveSpeed;//水纹速度
    
    CGFloat waterWaveHeight; //水纹高度
    CGFloat waterWaveWidth; //水纹宽度
    
}
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.layer.masksToBounds  = YES;
        [self setUp];
    }
    
    return self;
}

-(void)setUp
{
    waterWaveHeight = 15;
//    waterWaveHeight = self.frame.size.height/2;
    waterWaveWidth = self.frame.size.width;
//    _firstWaveColor = [UIColor colorWithRed:223/255.0 green:22/255.0 blue:64/255.0 alpha:1];
//    _secondWaveColor = [UIColor colorWithRed:255 green:255 blue:0 alpha:1];
    
    _firstWaveColor = [UIColor getColorWithHexNumber:@"F1F1EE"];
    _secondWaveColor = [UIColor getColorWithHexNumber:@"FD8599"];

//    waveGrowth = 0.85;
    waveSpeed = 0.4/M_PI;
    
    [self startWare];
}


-(void)serFirstWaveColor:(UIColor *)firstWaveColor
{
    _firstWaveColor = firstWaveColor;
    _firstWaveLayer.fillColor = firstWaveColor.CGColor;
}

-(void)setSecondWaveColor:(UIColor *)secondWaveColor
{
    _secondWaveColor = secondWaveColor;
    _secondWaveLayer.fillColor = secondWaveColor.CGColor;
    
}



-(void)startWare
{
    
    if (_firstWaveLayer == nil) {
        _firstWaveLayer = [CAShapeLayer layer];
        _firstWaveLayer.fillColor = _firstWaveColor.CGColor;
        [self.layer addSublayer:_firstWaveLayer];
    }
    
    waveSpeed = 1;
    waveA = 10;
    waveW = 1/30.0;
    currentK = self.frame.size.height/2 ;
    //启动定时器
    _waveDisplaylink = [CADisplayLink displayLinkWithTarget:self selector:@selector(getCurrentWave:)];
    [_waveDisplaylink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
    
    if (_secondWaveLayer == nil) {
        _secondWaveLayer = [CAShapeLayer layer];
        _secondWaveLayer.fillColor = _secondWaveColor.CGColor;
        [self.layer addSublayer:_secondWaveLayer];
    }
    
    waveSpeed = 0.6;
    waveA = 10;
    waveW = 1/30.0;
    currentK = self.frame.size.height/2 ;
    //启动定时器
    _waveDisplaylink = [CADisplayLink displayLinkWithTarget:self selector:@selector(getCurrentWave1:)];
    [_waveDisplaylink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}


-(void)getCurrentWave:(CADisplayLink *)displayLink
{
    
    //波浪位移
    offsetX += waveSpeed;
    
    [self setCurrentFirstWaveLayerPath];
}

-(void)getCurrentWave1:(CADisplayLink *)displaLink
{
    offsetX += waveSpeed;
    
    [self setCurrentSecondWaveLayerPath];
}

-(void)setCurrentFirstWaveLayerPath
{
    CGMutablePathRef path = CGPathCreateMutable();
//    CGFloat y = currentK ;
    CGFloat y = waterWaveHeight*sinf(offsetX*M_PI/waterWaveWidth);
    CGPathMoveToPoint(path, nil, 0, y);
    for (float x = 0.0f; x<=waterWaveWidth; x++) {
        //正玄波浪公式
//        y = waveA * sin(waveW * x+ offsetX)+currentK;
        y = 1.1 *waterWaveHeight*sinf(2.5*M_PI *x/waterWaveWidth + offsetX *M_PI/waterWaveWidth)+currentK-8;
        CGPathAddLineToPoint(path, nil, x, y);
    }
    CGPathAddLineToPoint(path, nil, waterWaveWidth, self.frame.size.height);
    CGPathAddLineToPoint(path, nil, 0, self.frame.size.height);
    CGPathCloseSubpath(path);
    
    _firstWaveLayer.path = path;
    
    
    CGPathRelease(path);
}

-(void)setCurrentSecondWaveLayerPath
{
    CGMutablePathRef path1 = CGPathCreateMutable();
//    CGFloat y = currentK;
    CGFloat y1 = waterWaveHeight *sinf(offsetX*M_PI/waterWaveWidth + M_PI/4);
    CGPathMoveToPoint(path1, nil, 0, y1);
    for (float x = 0.0f; x<=waterWaveWidth; x++) {
        //正玄波浪公式
//        y = waveA * sin(waveW * x+ offsetX ) +currentK;
        y1 = waterWaveHeight*sinf(2.5*M_PI*x/waterWaveWidth + 3*offsetX*M_PI/waterWaveWidth + M_PI/4)+currentK;
        CGPathAddLineToPoint(path1, nil, x, y1);
    }
    CGPathAddLineToPoint(path1, nil, waterWaveWidth, self.frame.size.height);
    CGPathAddLineToPoint(path1, nil, 0, self.frame.size.height);
    CGPathCloseSubpath(path1);
    
    _secondWaveLayer.path = path1;
        CGPathRelease(path1);
}



@end
